#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
int a[MAXN], p[MAXN], fa[MAXN], sum[MAXN];
int ans = 0;

int vis[MAXN];

void init(int n) {
  for (int i = 1; i <= n; ++i) {
    fa[i] = i;
  }
}

int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); }

void merge(int i, int j) {
  int x = find(i), y = find(j);
  if (x != y) {
    fa[x] = y;
    sum[y] += sum[x];
  }
}

int he(int x) {
  int zuzong = find(x);
  return sum[zuzong];
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = a[i];
  }
  for (int i = n; i >= 1; i--) {
    cin >> p[i];
  }
  init(n);

  for (int i = 1; i <= n; ++i) {
    int x = p[i];
    
    vis[x] = true; 
    if (vis[x - 1])
      merge(x, x - 1);

    if (vis[x + 1])
      merge(x, x + 1);

    int s = he(x);
    if (s > ans) {
      ans = s;
    }
    cout << ans << " ";
  }

  return 0;
}